package day17.zhiyou100;

import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.util.UUID;

public class Demo09_CommonApache {

    // 把D:盘中的\\aa\\bb\\ww.txt文件上传到E:盘中的dd文件夹中，名称唯一不重复
    public static void m1() throws Exception {
        File file = new File("D:\\aa\\bb\\ww.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String extension = FilenameUtils.getExtension(file.getAbsolutePath());
        String pan = FilenameUtils.getPrefix(file.getAbsolutePath());
        String pathname = pan+UUID.randomUUID().toString().replace("-","") + "." + extension;
        //e2a7a19d-480d-4d3c-b653-71fd9578e829.zip
        File file2 = new File(pathname);
        FileWriter fw = new FileWriter(file2);
        BufferedWriter bw = new BufferedWriter(fw);

        // 边读边写
        String text;
        while ((text = br.readLine()) != null) {
            bw.write(text);
        }
        //刷新
        bw.flush();
        // 关闭
        bw.close();
        fw.close();
        br.close();
        fr.close();

    }

    public static void m2() throws Exception {
        // FilenameUtils 文件名称工具类
        File file = new File("E:\\dd\\aa.txt");

        // getName()
        String name = FilenameUtils.getName(file.getAbsolutePath());//
        System.out.println(name);//
        // getExtension()
        String extension = FilenameUtils.getExtension(file.getAbsolutePath());
        System.out.println(extension);//

        // 判断后缀名
        if (FilenameUtils.isExtension(file.getAbsolutePath(),"txt")){
            System.out.println("该文件的扩展名为txt");
        }

        String prefix = FilenameUtils.getPrefix(file.getAbsolutePath());
        System.out.println(prefix);// 盘符
    }
    public static void main(String[] args) throws Exception {
        m1();
    }
}
